package com.caigou.servlet;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import sch.jl.caigou.utils.Page;

import jiliang.purchase.demo.BusinessProcessing;
import jiliang.purchase.demo.CriteriaBusinessProcess;
import jiliang.purchase.demo.PersonObject;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import com.caigou.service.BusinessProcessService;

public class ExportExcelServlet extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	private BusinessProcessService BPS = new BusinessProcessService();
	private CriteriaBusinessProcess CBP = null;
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/vnd.ms-excel;charset=utf-8");

		String filedisplay = "list.xls";
		response.addHeader("Content-Disposition", "attachment;filename="+ filedisplay);
		PersonObject PO = new PersonObject();
		PO = (PersonObject)session.getAttribute("Person");
		
		String xmid = null;
		String bh = null;
		String xingming = null;
		String start = null;
		String endtime = null;
		String zt = null;
		String pageNo = request.getParameter("page");
		int pageNum = 1;
		String returnTitle = "";
		if(pageNo != null && !pageNo.equals(""))
		{
			pageNum = Integer.valueOf(pageNo);
		}
		
		if(request.getParameter("xmid")!=null && !request.getParameter("xmid").equals("0"))
		{
			xmid = request.getParameter("xmid");
		}
		//System.out.println(xmid);
		
		if(request.getParameter("cbh")!=null && !request.getParameter("cbh").equals(""))
		{
			bh = request.getParameter("cbh");
		}
		else
		{
			returnTitle+="a"+"|";
		}
		//System.out.println(bh);
		if(request.getParameter("xingming")!=null && !request.getParameter("xingming").equals(""))
		{
			xingming = request.getParameter("xingming");
			xingming = java.net.URLDecoder.decode(xingming, "UTF-8");
		}
		else
		{
			returnTitle+="b"+"|";
		}
		//System.out.println(xingming);
		
		//System.out.println(xingming);
		if(request.getParameter("starttime")!=null && !request.getParameter("starttime").equals(""))
		{
			start = request.getParameter("starttime");
		}
		else
		{
			returnTitle+="c"+"|";
		}
		//System.out.println(start);
		if(request.getParameter("endtime")!=null && !request.getParameter("endtime").equals(""))
		{
			endtime = request.getParameter("endtime");
		}
		else
		{
			returnTitle+="d"+"|";
		}
		//System.out.println(endtime);
		if(request.getParameter("zt")!=null && !request.getParameter("zt").equals("0"))
		{
			zt = request.getParameter("zt");
		}
		else
		{
			returnTitle+="0";
		}
		//System.out.println(zt);
		CBP = new CriteriaBusinessProcess(xmid,bh,xingming,start,endtime,zt,pageNum);
		
		List<BusinessProcessing> list =new ArrayList<BusinessProcessing>();
		
		list = BPS.getList(PO.getJs_id(), CBP);
		//System.out.println(list.size());
		
		try {
			writeExcel(response.getOutputStream(),list);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request,response);
	}
	
	public void writeExcel(OutputStream out,List<BusinessProcessing> list)throws Exception
	{
		WritableWorkbook book = Workbook.createWorkbook(out);
		WritableSheet sheet = book.createSheet("sheet1", 0);
		
		String[] title={"流水号","项目名称","姓名","申请时间","上一流程","下一流程"};
		
		for(int i=0;i<title.length;i++)
		{
			Label label = new Label(i,0,title[i]);
			sheet.addCell(label);
		}
		
		for(int i=1;i<list.size();i++)//行
		{	
				Label label1 = new Label(0,i,list.get(i).getBh());
				//jxl.write.Number number = new jxl.write.Number(i,0,Double.valueOf(list.get(i).getBh())); 
				sheet.addCell(label1);
				Label label = new Label(1,i,list.get(i).getXm_mingcheng());
				sheet.addCell(label);
				label = new Label(3,i,list.get(i).getXingming());
				sheet.addCell(label);
				label = new Label(4,i,list.get(i).getSq_shijian());
				sheet.addCell(label);
				label = new Label(5,i,list.get(i).getLast_lc());
				sheet.addCell(label);
				label = new Label(6,i,list.get(i).getBz_mc());
				sheet.addCell(label);
		}		
			book.write();
			book.close();
	}

}
